package com.example.controller;

import com.example.service.IUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author lyj
 * @Title: UserController
 * @ProjectName demo
 * @Description: TODO
 * @date 2020/12/3 23:17
 */

@Controller
public class UserController {
    @Autowired
    private IUserService userService;
    @RequestMapping("/test")
    public String test(Model model){
        model.addAttribute("name","黑马");
        System.out.println(userService.list());
        return "test";
    }

    @RequestMapping("/add")
    public String add(Model model){
        return "user/add";
    }

    @RequestMapping("/update")
    public String update(Model model){
        return "user/update";
    }

    @RequestMapping("/toLogin")
    public String tologin(Model model){
        return "login";
    }

    @RequestMapping("/noAuth")
    public String noAuth(Model model){
        return "noAuth";
    }

    @RequestMapping("/login")
    public String login(String username,String password,Model model){
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token=new UsernamePasswordToken(username,password);
        try {
            subject.login(token);
            return "test";
        }catch (UnknownAccountException e){
          //  e.printStackTrace();
            model.addAttribute("msg","用户名不存在");
            return "login";
        }catch (IncorrectCredentialsException e){
            model.addAttribute("msg","密码错误");
            return "login";
        }

    }

}
